package com.example.kingdee.kingdeeBridge.kingdeeImpl.impl;

import cn.hutool.core.util.NumberUtil;
import com.example.kingdee.common.StringUtils;
import com.example.kingdee.exception.ProcedureException;
import com.example.kingdee.kingdeeBridge.common.RequestResult;
import com.example.kingdee.kingdeeBridge.dto.BillWriteInDto;
import com.example.kingdee.kingdeeBridge.entity.SrcBillEntry;
import com.example.kingdee.kingdeeBridge.kingdeeImpl.BillImpl;
import com.example.kingdee.mapper.TBxStMapper;
import com.example.kingdee.sqlTool.SqlTool;
import com.example.kingdee.sqlTool.common.DataObject;
import com.example.kingdee.sqlTool.common.QueryObject;
import com.example.kingdee.sqlTool.sqltoolEnum.FormIdEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.*;

/**
 * 检验单接口实现类
 */
@Service
public class InspectBillImpl implements BillImpl {

    @Autowired
    private SqlTool sqlTool;

    @Autowired
    private TBxStMapper tBxStMapper;

    @Override
    public Map<String, String> selectTargetBillEntry(String fid) {
        //查询明细
        QueryObject inventorySelect = new QueryObject(FormIdEnum.qmInspect);
        ArrayList<String> resultKeyArray = new ArrayList<>();
        inventorySelect.addSelect("FEntity_FEntryID");//明细行id
        resultKeyArray.add("entryId");
        inventorySelect.addSelect("FSrcEntryId0");//原单分录内码
        resultKeyArray.add("srcEntryId");
        inventorySelect.addCondition("fid", fid);
        ArrayList<HashMap<String, Object>> targetEntryInfo = sqlTool.weaveResult(inventorySelect, resultKeyArray);
        //明细行id和原单行id对应关系
        HashMap<String, String> keyValue = new HashMap<>();
        for (HashMap<String, Object> sld : targetEntryInfo) {
            keyValue.put(sld.get("srcEntryId").toString(), sld.get("entryId").toString());
        }
        return keyValue;
    }

    @Override
    public RequestResult updateTargetBillEntry(String fid, BillWriteInDto billWriteInDto, Map<String, String> targetInfo) {
        List<SrcBillEntry> srcBillEntryList = billWriteInDto.getSrcBillEntryList();
        //将源单明细按照行id进行分类
        MultiValueMap<String, SrcBillEntry> classFySrcBillEntry = sqlTool.getClassFy(srcBillEntryList);
        //修改交货数量
        List<Map<String, Object>> detailEntityList = new ArrayList<>();
        for (String entryId : targetInfo.keySet()) {
            List<SrcBillEntry> srcBillEntries = classFySrcBillEntry.get(entryId);
            //计算总不合格
            double qtyAll = 0.0;
            double okQtyAll = 0.0;
            for (SrcBillEntry map : srcBillEntries) {
                //不合格数
                double creditPrice = map.getReserved1() == null ? 0.0 : Double.parseDouble(map.getReserved1().toString()) ;
                //合格数
                double realQty = map.getReserved2() == null ? 0.0 : Double.parseDouble(map.getReserved2().toString()) ;
                qtyAll = NumberUtil.add(qtyAll, creditPrice);
                okQtyAll = NumberUtil.add(okQtyAll, realQty);
            }
            //获取检验结果
            Object inspectResult = srcBillEntries.get(0).getReserved3();
            //构建明细行数据
            Map<String,Object> detailEntity = new LinkedHashMap<>();
            detailEntity.put("FEntryID", targetInfo.get(entryId));//行id 必填
            detailEntity.put("FUnqualifiedQty", qtyAll);//不合格数量
            detailEntity.put("FInspectResult", inspectResult);//检验结果
            //构建使用决策
            List<Map<String,Object>> policyList = new ArrayList<>();
            //获取使用决策
            Object decision = srcBillEntries.get(0).getReserved4();
            if (okQtyAll !=0 ){
                //合格数量
                Map<String,Object> policyDetailOk = new LinkedHashMap<>();
                policyDetailOk.put("FPolicyStatus", "1");//使用决策-状态
                policyDetailOk.put("FPolicyQty", okQtyAll);//使用决策-数量
                policyDetailOk.put("FUsePolicy", "A");//使用决策-使用决策
                policyList.add(policyDetailOk);
            }
            if (qtyAll !=0){
                //不合格数量
                Map<String,Object> policyDetailNg = new LinkedHashMap<>();
                policyDetailNg.put("FPolicyStatus", "2");//使用决策-状态
                policyDetailNg.put("FPolicyQty", qtyAll);//使用决策-数量
                policyDetailNg.put("FUsePolicy", decision);//使用决策-使用决策
                policyList.add(policyDetailNg);
            }
            if (policyList.size() != 0){
                detailEntity.put("FPolicyDetail", policyList);
            }
            detailEntityList.add(detailEntity);
        }
        //修改收料通知单
        DataObject order = new DataObject(FormIdEnum.qmInspect);
        order.setAttribute("FID", fid);
//        order.setAttribute("FInspectOrgId", fid);
        order.setAttribute("FEntity", detailEntityList);//明细

        try {
            Map<String, Object> result = sqlTool.editForDataObject(order);
            if (result.get("code").toString().equals("200")){
                return RequestResult.ofSuccess(result);
            }else {
                return RequestResult.ofError(result.get("msg").toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new ProcedureException("无法调用云星空接口");
        }
    }

    @Override
    public void postPosition(String billNo) {

    }

    @Override
    public void auditTargetBill(String billNo) {
        //提交审核
        sqlTool.auditBill(billNo, FormIdEnum.qmInspect);
    }

    @Override
    public void deleteTargetBill(String fid) {
        sqlTool.deleteBill(fid, FormIdEnum.qmInspect);
    }
}
